# -*- coding: utf-8 -*-
##zixue@taobao.com
##2008-6-16

import cgi
import os
import string
import time
import wsgiref.handlers

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.api import memcache

from TbApi import *
from HttpClient import HttpClient
from AEProxy import *
from Log import Log
Log.enableDebug = True


def getPageHeader(nick, logoutUrl):
	header = """<div>您好，%s！<a href="%s">[退出]</a> | 
		您绑定的淘宝帐号是： <a href="">[更换]</a> | 
		<a href="/">[首页]</a>
		<a href="/trade/sold">[已卖出]</a>
		<a href="/trade/bought">[已买到]</a>
		<a href="/item/onsale">[出售中]</a>
		<a href="/item/instock">[仓库内]</a>
		<a href="/item/get">[宝贝查询]</a>
		<a href="/item/update">[宝贝更新]</a></div>"""
	return header % (nick, logoutUrl);

def getPageRedirect(redirectUrl):
	redirect = """<div><a href="%s">点此允许本网站访问您的隐私数据</a>
				<div>
				<br/>
				点击上面的链接登录淘宝后,<br/>
				您既可授权本网站在30分钟内访问您的隐私数据.
				<br/>
				</div>"""
	return redirect % str(redirectUrl)

def checkLogin(handler, app_path):
	#XXX Use gmail username as sessionId
	user = users.get_current_user()
	if user is None:
		handler.redirect(users.create_login_url(app_path))
	return user

def  getSipLoginUrl(login, redirect):
	return login + "&" + urllib.urlencode({'sip_redirecturl':redirect})


class AppListController(webapp.RequestHandler):
	app_path = '/'
	view_name = 'AppList.html'

	def get(self):
		user = checkLogin(self, self.app_path)
		if user is None: return
		template_values = {
			'page_header':getPageHeader(user.nickname(),users.create_logout_url(self.app_path)),
			'page_title':'淘宝应用程序列表',
			'login':user,
		}
		path = os.path.join(os.path.dirname(__file__), self.view_name)
		self.response.out.write(template.render(path, template_values))

def main():
	application = webapp.WSGIApplication([(AppListController.app_path, AppListController)])
	wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
	main()